<?php
namespace frontend\controllers;

use app\models\Category;
use app\models\Customer;
use app\models\Order;
use app\models\Orderdetail;
use app\models\User;
use Yii;
use yii\db\Exception;

/**
 * Site controller
 */
class SiteController extends BaseController
{
        public function actionOperatorLogin(){
            $this->layout="login";
            return $this->render("operator-login");
        }
        public function actionLoginOperator(){
            $request = Yii::$app->request;
            $return = [];
            if($request->isAjax){
                $username = $request->post("username");
                $password = $request->post("password");
                $user = User::findOne(['username'=>$username]);
                if($user){
                    if(Yii::$app->security->validatePassword($password,$user->password)){
                        Yii::$app->user->login($user);
                        $return['status']=true;

                    }else{
                        $return['status']=false;
                        $return['msg']="用户名或密码错误";

                    }
                }else{
                    $return['status']=false;
                    $return['msg']="用户名或密码错误";


                }
            }
            return json_encode($return);
        }
        public function actionChooseCate(){
            $cates= Category::find()->where(['p_id'=>0,'flag'=>1])->asArray()->all();

            return $this->render("choose-cate",['cates'=>$cates]);
        }

        public function actionRefuse(){
            $request = Yii::$app->request;
            $cateId=$request->get("cateId");

            $cate = Category::findOne($cateId);
            if($cate->tpl==1){
                $this->layout="star";
                return $this->render("kitchen");
            }else{
                $cates = Category::find()->where(['p_id'=>$cateId,'flag'=>1])->asArray()->all();

                return $this->render("recoverable",["cates"=>$cates]);
            }


        }
        public function actionCreateOrder(){
            $request = Yii::$app->request;
            if($request->isPost){
              $tr= Yii::$app->db->beginTransaction();
                try{
                    $customers =array_unique( $request->post("customer"));
                    foreach ($customers as $cus){
                        $order = new Order();
                        $customer = Customer::findOne($cus);
                        $user = User::findOne(Yii::$app->user->getId());
                        $order->customer_id=$customer->id;
                        $order->customer_name=$customer->realname;
                        $order->user_id=$user->id;
                        $order->user_name=$user->realname;
                        $order->create_time=time();
                        if($order->save()){

                        }else{
                            $tr->rollBack();
                            return $this->render("error");
                        }

                        $items=['order_id','type','number','score'];
                        $values=[];
                        $list=$request->post("list");
                        foreach ($list as $v){
                            $score = isset($v['score'])?$v['score']:0;
                            $values[]=[$order->id,$v['cate'],$v['number'],$score];
                        }
                        if(Yii::$app->db->createCommand()->batchInsert(Orderdetail::tableName(), $items, $values)->execute()){

                        }else{
                            $tr->rollBack();
                            return $this->render("error");
                        }

                        $customer->blue=$customer->blue+1;
                        if($customer->save()){

                        }else{
                            $tr->rollBack();
                            return $this->render("error");
                        }
                    }
                    $tr->commit();
                    return $this->render("success");

                }catch (Exception $e){
                    $tr->rollBack();
                    return $this->render("error");
                }


            }
        }
        public function actionSuccess(){
            return $this->render("success");
        }
        public function actionGetCustomerInfo(){
            $request = Yii::$app->request;
            if($request->isAjax){
                $return = [];
                $customerId=$request->post("customerId");
                $customer = Customer::findOne($customerId);
                if($customer&&isset($customer)){
                    $return["status"]=true;
                    $return['info']['id']=$customer->id;
                    $return["info"]['realname']=$customer->realname;
                    $return['info']['phone']=$customer->phone;
                    $return['info']['address']=$customer->address;
                    $return['info']['blue']=$customer->blue;
                    $return['info']['green']=$customer->green;
                }else{
                    $return['status']=false;
                    $return['msg']="不存在的用户";
                }
                return json_encode($return);
            }
        }

}
